package services

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"grpc-comment/model"
	. "grpc-comment/protos/comment"
	"grpc-comment/repo"
	"math"
	"time"
)

type CommentInterfaceService interface {
	AddComment(ctx context.Context, request *AddCommentRequest) (*AddCommentResponse, error) //添加评论
	GetComment(ctx context.Context, request *GetCommentRequest) (*GetCommentResponse, error) //获取评论
	DelComment(context.Context, *DelCommentRequest) (*DelCommentResponse, error) //删除评论
}

type UserDetail struct{
	Id string `json:"id"`
	Uid  string `json:"uid"`
	Type  int32 `json:"type"`
	Nickname string `json:"nickname"`
	HeadImg string`json:"head_img"`
	IsAdmin int32 `json:"is_admin"`
	IsVip int32 `json:"is_vip"`
	Phone string`json:"phone"`
	Password string `json:"password"`
	Status int32`json:"status"`
	Reward int32`json:"reward"`
}

//初始化对象函数
func NewCommentService() CommentInterfaceService {
	return &CommentService{
		commentService:       repo.NewComment(),
		memberService:       repo.NewMember(),
	}
}

type CommentService struct {
	commentService 			    repo.CommentInterface           //评论服务
	memberService 			    repo.MemberInterface           //用户服务
}

//添加评论
func (this *CommentService) AddComment(ctx context.Context, request *AddCommentRequest) (*AddCommentResponse, error){
	fmt.Println("AddComment req:",request)
	fmt.Println("Comment:",request.Comment)
	var comm model.Comment
	comm.ID = request.Comment.Id
	comm.Content = request.Comment.Content
	comm.CreateTime = time.Now().Unix()
	comm.Pid = int(request.Comment.Pid)
	comm.Ouid = int(request.Comment.Ouid)
	comm.Postid = int(request.Comment.PostId)
	err := this.commentService.AddComment(comm)
	if err != nil {
		return nil, err
	}
	return &AddCommentResponse{Success:true,Err: &ErrResponse{Type:"success",Msg:"添加成功"}},nil
}


//实现GetComment
func (this *CommentService) GetComment(ctx context.Context, request *GetCommentRequest) (*GetCommentResponse, error) {
	var comments []*Comment
	//如果没有分页，默认是第一页和显示20条
	var page int64 = 1
	var perPage int64 = 20
	params := make(map[string]interface{})
	params["postid"] = request.PostId
	lists,total,err := this.commentService.GetComment(params, page, perPage)
	if err != nil {
		return nil, err
	}
	//comments := make([]*Comment,len(lists))
	for _,val := range lists {
		var comment Comment
		var user UserDetail
		userInfo,_ :=this.memberService.GetUserInfo(int64(val.Ouid))
		_,ok := userInfo["id"]
		if !ok{
			return nil, errors.New("map解析失败！")
		}
		// 序列化
		arr, err := json.Marshal(userInfo)
		if err != nil {
			panic(err)
		}
		// 反序列化
		err2 := json.Unmarshal(arr, &user)
		if err2 != nil {
			panic(err2)
		}
		////使用mapstructure.Decode()方法
		//err := mapstructure.Decode(userInfo,&user)
		//if err != nil {
		//	fmt.Println(err.Error())
		//}
		fmt.Println("user:",user)
		comment.Id = val.ID
		comment.Ouid = int64(val.Ouid)
		comment.LikeNum = int32(val.Like)
		comment.Pid = int32(val.Pid)
		comment.PostId = int64(val.Postid)
		comment.Content = val.Content
		comment.CreateTime = val.CreateTime
		comment.CreateDate = time.Unix(val.CreateTime,0).Format("2006-01-02 15:04:05")
		comment.HeadImg = user.HeadImg
		comment.Nickname = user.Nickname
		comment.IsAdmin = int64(user.IsAdmin)
		comment.IsVip = int64(user.IsVip)
		comments = append(comments,&comment)
	}
	return &GetCommentResponse{
		Rows: comments,
		Count: int64(len(comments)),
		Total: int64(total),
		Pages: int64(math.Round(float64(total)/float64(perPage)) + 1),
		Page: page,
		PerPage: perPage,
	},nil
}

//删除评论
func (this *CommentService) DelComment(ctx context.Context, request *DelCommentRequest) (*DelCommentResponse, error) {
	fmt.Println("DelCommentRequest:",request)
	fmt.Println("id:",request.Id)
	return &DelCommentResponse{Success: true},nil
}

